package com.joplus.housekeeper.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * author： ccm
 * time： 2017/6/27.
 * description：
 */

public class CalendarUtils {


    public static int validTimeCount(String time) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            int curMonth = calendar.get(Calendar.MONTH);
            int curYear = calendar.get(Calendar.YEAR);
            Date parse = simpleDateFormat.parse(time);
            calendar.setTime(parse);
            int monthCount = (calendar.get(Calendar.YEAR) - curYear) * 12 + (calendar.get(Calendar.MONTH) - curMonth) ;
            return monthCount;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static int pastTimeCount(String time) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            int curMonth = calendar.get(Calendar.MONTH);
            int curYear = calendar.get(Calendar.YEAR);
            Date parse = simpleDateFormat.parse(time);
            calendar.setTime(parse);
            int monthCount = (curYear - calendar.get(Calendar.YEAR)) * 12 + curMonth - (calendar.get(Calendar.MONTH));
            return monthCount;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }


}
